//
//  LGDietRecordViewController.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/3/11.
//  饮食记录

#import "LGDietRecordViewController.h"
#import "LGDietRecordCell.h"

@interface LGDietRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIButton *leftButton;
/**   */
@property (nonatomic , strong)UIButton *rightButton;
/**   */
@property (nonatomic , strong)UILabel *monthLabel;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;
/**   */
@property (nonatomic , copy)NSString *currentMonth;

@end

@implementation LGDietRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentMonth = [self.formatter stringFromDate:[NSDate date]];
    [self.view addSubview:self.leftButton];
    [self.view addSubview:self.monthLabel];
    [self.view addSubview:self.rightButton];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Sportsprescription.NutritionPrescription/recordList" parameters:@{@"patient_id":self.patientid,@"date_range":self.currentMonth} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:0];
        }
    }];
}

-(void)setCurrentMonth:(NSString *)currentMonth{
    _currentMonth = currentMonth;
    NSString *content = [currentMonth stringByReplacingOccurrencesOfString:@"-" withString:@"年"];
    self.monthLabel.text = [NSString stringWithFormat:@"%@月",content];
}

-(void)monthButtonAction:(UIButton *)sender{
    NSInteger month = (sender == self.leftButton)?-1:1;
    NSDate *currentDate = [self.formatter dateFromString:self.currentMonth];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// NSGregorianCalendar
    NSDate *toDate = [calender dateByAddingComponents:components toDate:currentDate options:0];
    self.currentMonth = [self.formatter stringFromDate:toDate];
    [self requestData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"lists"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGDietRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *tempArray = self.dataArray[indexPath.section][@"lists"];
    cell.dataDic = tempArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(40);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    headView.topMargin = viewPix(8);
    UILabel *dateLabel = [UILabel lableWithText:self.dataArray[section][@"date_str"] colorString:@"#000000" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    dateLabel.frame = CGRectMake(viewPix(28), viewPix(20), Screen_W-viewPix(56), viewPix(20));
    [headView addSubview:dateLabel];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.bottomMargin = viewPix(8);
    return footView;
}


#pragma mark
#pragma mark ====> 创建控件
-(UIButton *)leftButton{
    if(!_leftButton){
        _leftButton = [UIButton buttonWithImage:@"日历-上个月" frame:CGRectMake(Screen_W/2.0-viewPix(100), 0, viewPix(42), viewPix(42))];
        [_leftButton addTarget:self action:@selector(monthButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

-(UIButton *)rightButton{
    if(!_rightButton){
        _rightButton = [UIButton buttonWithImage:@"日历-下个月" frame:CGRectMake(Screen_W/2.0+viewPix(58), 0, viewPix(42), viewPix(42))];
        [_rightButton addTarget:self action:@selector(monthButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

-(UILabel *)monthLabel{
    if(!_monthLabel){
        _monthLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _monthLabel.frame = CGRectMake(Screen_W/2.0-viewPix(50), 0, viewPix(100), viewPix(42));
    }
    return _monthLabel;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(44), Screen_W, Screen_H-topBarHeight-viewPix(88)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGDietRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"yyyy-MM";
    }
    return _formatter;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
